#include "input_system.h"
#include "debug.h"
#include <SDL/SDL.h>
#include <SDL/SDL_keyboard.h>
#include <SDL/SDL_mouse.h>
/*--------------------------------------------------------------------------------*/
IMPLEMENT_SINGLETON( INPUT_SYSTEM );
/*--------------------------------------------------------------------------------*/
INPUT_SYSTEM::INPUT_SYSTEM() :
	OBJECT(),
	CurrentKeyStateTable(),
	PreviousKeyStateTable(),
	CurrentText(),
	TextCatchingIsOn( false )
{

}
/*--------------------------------------------------------------------------------*/
INPUT_SYSTEM::~INPUT_SYSTEM()
{
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::Initialize()
{
	DEBUG_LogLine( "INPUT_SYSTEM::Initialize()" );
	CurrentKeyStateTable.Clear();
	PreviousKeyStateTable.Clear();
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::Update( const TIMER & /*time_step*/ )
{
	UINT8
		*buffer;
	INT
		key_count;
	UINT8
		mouse_state;
	INT
		mouse_x,
		mouse_y;

	buffer = SDL_GetKeyState( &key_count );

	PreviousKeyStateTable = CurrentKeyStateTable;

	memcpy( CurrentKeyStateTable.GetArray(), buffer, sizeof( UINT8 ) * key_count );

	if( TextCatchingIsOn )
	{
		CatchText();
	}

	mouse_state = SDL_GetMouseState( & mouse_x, & mouse_y );

	PreviousCursorPosition = CurrentCursorPosition;
	CurrentCursorPosition.X = mouse_x;
	CurrentCursorPosition.Y = mouse_y;
}
/*--------------------------------------------------------------------------------*/
BOOL INPUT_SYSTEM::IsKeyPressed( INT key ) const
{
	return CurrentKeyStateTable[ key ];
}
/*--------------------------------------------------------------------------------*/
BOOL INPUT_SYSTEM::IsKeyReleased( INT key ) const
{
	return !CurrentKeyStateTable[ key ];
}
/*--------------------------------------------------------------------------------*/
BOOL INPUT_SYSTEM::IsKeyJustPressed( INT key ) const
{
	return CurrentKeyStateTable[ key ] && !PreviousKeyStateTable[ key ];
}
/*--------------------------------------------------------------------------------*/
BOOL INPUT_SYSTEM::IsKeyJustReleased( INT key ) const
{
	return !CurrentKeyStateTable[ key ] && PreviousKeyStateTable[ key ];
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::StartTextCatcher()
{
	CurrentText = "";
	TextCatchingIsOn = true;
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::EndTextCatcher()
{
	TextCatchingIsOn = false;
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::SetCurrentKeyState( INT key, BOOL state )
{
	CurrentKeyStateTable[ key ] = state;
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::SetPreviousKeyState( INT key, BOOL state )
{
	PreviousKeyStateTable[ key ] = state;
}
/*--------------------------------------------------------------------------------*/
VOID INPUT_SYSTEM::CatchText()
{
	INDEX
		index;

	for_each_index( index, 26 )
	{
		if( IsKeyJustPressed( INPUT_KEY_A + index ) )
		{
			CurrentText += CHAR( 'a' + index );
		}
	}

	if( IsKeyJustPressed( INPUT_KEY_Space ) )
	{
		CurrentText += ' ';
	}

	if( IsKeyJustPressed( INPUT_KEY_Backspace ) && CurrentText.GetLength() > 0 )
	{
		CurrentText.RemoveLastChars( 1 );
	}

	if( IsKeyJustPressed( INPUT_KEY_Space ) )
	{
		CurrentText += ' ';
	}
}
/*--------------------------------------------------------------------------------*/
